<?php
class MY_Controller extends CI_Controller
{
	// 判断是否登录
	public $islogin = false;

	public $_d = array();

	// 每页条数
	public $perpages = 20;

	// 分页初始化
	public $_p = array();


	public function __construct()
    {
        parent::__construct();
		$this->load->library('user_agent');
		$this->isLogin();
		$this->initComm();

//		if (($this->agent->is_mobile() == '1') && ($this->uri->segment(1) != 'mobile')) redirect('mobile/home');

		$this->_p = array('pagenumb'=>$this->perpages,
							'pagecur'=>($this->input->post('pagecur') == 1) ? 0 : $this->input->post('pagecur'),
							'pagecount'=>$this->input->post('pagecount'));
		
    }

	public function initComm()
	{
		$this->lang->load('default', 'cn');	
		$this->_d['lg'] = '';
		$this->load->model('Config_model', 'conf');
		$this->load->model('Category_model', 'c');
		$this->load->model('Advertisement_model', 'ad');

		// 网页配置项
		$retdata = $this->conf->L(array(),'*',0,0);
		foreach ($retdata as $k => $v) $data[$v['confkey']] = $v['confval'];

		$this->_d['cfg'] = $data;
		
		// 菜单
		$data = $this->c->L(array('func'=>'menu',"status"=>'1'),'*',0,0,'sort');
		if ($this->_d['cfg']['tpl'] == 'themes/ey_tpl_2018/'){
			$newArray = array_reduce($data, function(&$newArray,$v){
				$newArray[$v['cateid']] = $v;
				return $newArray;
			});
			$this->_d['menu'] = set2Tree($newArray);
		}else{		
			foreach ($data as $k => $v)
			{
				$_t = array();
				$_t = explode("/",$v['alias']);
				$data[$k]['menu_name'] = $_t[count($_t)-1];
			}
			$this->_d['menu'] = $data;
			$this->_d['alias'] = '';
		}
		//print_r($this->_d['pcate']);
		// 版块
		$ad_contact = $this->ad->O(array('cateid'=>'274','ispublic'=>'1','status'=>'1'));
		$ad_pic = $this->ad->L(array('cateid'=>'263','ispublic'=>'1','status'=>'1'),'imgthumb,link',0,0,'sort');
		$ad_link = $this->ad->L(array('cateid'=>'280','ispublic'=>'1','status'=>'1'),'imgthumb,adtype,title,link',0,0,'sort');
		if (!empty($this->_d['lg']) && $this->_d['lg'] == 'en')
		{
			$ad_contact = $this->ad->O(array('cateid'=>'262','ispublic'=>'1','status'=>'1'));
			$ad_pic = $this->ad->L(array('cateid'=>'261','ispublic'=>'1','status'=>'1'),'imgthumb,link',0,0,'sort');
			$ad_link = $this->ad->L(array('cateid'=>'274','ispublic'=>'1','status'=>'1'),'imgthumb,adtype,title,link',0,0,'sort');
		}
		$this->_d['ad_contact'] = $ad_contact;
		$this->_d['ad_pic'] = $ad_pic;
		$this->_d['ad_link'] = $ad_link;
	}

	// 设置页面缓存
	public function setCache($time = '')
	{
		if ($time == '') $time = intval($this->_d['cfg']['cache_time']);
		if ($time > 0) $this->output->cache($time/60);
	}

 
    public function isLogin()
    {
        $user = $this->session->userdata('userinfo');
		$this->_d['userinfo'] = $user;
        $this->islogin = ($user == false) ? false : true;
    }

	public function setAuthor($userdata)
	{
		if (count($userdata) > 0)
		{
			$this->session->set_userdata('userinfo', $userdata);
			$this->islogin = true;
		}
		else
		{
			$this->islogin = false;
		}
	}

	public function logout()
	{
		$this->session->unset_userdata('userinfo');
	}

	public function setAdminAuth($userdata)
	{
		$this->session->set_userdata('adminfo', $userdata);		
		$this->_d['adminfo'] = $userdata;
	}

	public function isAdmin()
	{
		return $this->session->userdata('adminfo');
	}

	public function adminLogout()
	{
		$this->session->unset_userdata('adminfo');
	}

	/**
 * 判断管理员对某一个操作是否有权限。
 *
 * 根据当前对应的action_code，然后再和用户session里面的action_list做匹配，以此来决定是否可以继续执行。
 * @param     string    $priv_str    操作对应的priv_str
 * @param     string    $msg_type       返回的类型
 * @return true/false
 */
public function admin_priv($priv_str)
{
	$adminfo = $this->isAdmin();
    if ($adminfo['action_list'] == 'all')
    {
        return true;
    }
    if (strpos($adminfo['action'], $priv_str) === false)
    {
        return false;
    }
    else
    {
        return true;
    }
}


public function action_log($isadmin,$action_userid,$action,$key='')
	{
		$this->load->model('Actionlog_model', 'aclog');
		$adata= array();
		$adata['userid'] = $action_userid;
		$adata['ctime'] = time();
		$adata['info'] = $action;
		$adata['ip'] = $this->input->ip_address();
		$adata['isadmin'] = $isadmin;
		if ($key != '')
		{
			$adata['info'] = $adata['info'] . "：". $key;
		}
		
		if ($this->aclog->A($adata))
		{
			return true;
		}
		else
		{
			return false;
		}


	}


//取得附件
public function getAttach($id='',$action='')
{
	$this->load->model('Attachedcate_model', 'ac');
	$this->load->model('Attacheddetail_model', 'attach');
	if (is_array($id))
	{
		$id = implode(",",$id);
	}

	$attachcateid = $this->ac->L(array('detailid in (' . $id . ')' =>'','action'=>$action),'*',0,0);

	if (!empty($attachcateid))
	{
		foreach ($attachcateid as $k => $v) $cateid[] = $v['attachcateid'];
		return $this->attach->L(array('attachcateid in (' . implode(",",$cateid) . ')' =>''),'*',0,0);
	}
	else
	{
		return false;
	}
	
}

}